#!/bin/bash

if [ ! -d "/etc/rancher/k3s" ]; then
	mkdir -p /etc/rancher/k3s
fi

if [ $# != 6 ];then

    echo "错误："
    echo "  a.安装Agent需要设置K3S_TOKEN参数值！！！"
    echo "  b.安装Agent需要设置Server的IP地址或域名！！！"
    echo "  c.请指定私有镜像仓库的 IP 和 端口 ！！！"
    echo "  d.请指定私有镜像仓库的 账号 和 密码 ！！！"
    echo "处理："
    echo "  请在 Server 服务器上执行\"cat /var/lib/rancher/k3s/server/node-token\"获取K3S_TOKEN，"
    echo "  然后如下格式重新执行："
    echo "    ./agent_install.sh \"k3s.s.com\" \"c9f56c820758218121acdd47439e8d89\" \"k3s.harbor.com\" 5050 \"zhangsan\" \"123456\""
    exit 1
fi

##server 服务器上执行：cat /var/lib/rancher/k3s/server/node-token 获取
export K3S_TOKEN="$2"

export INSTALL_K3S_SKIP_DOWNLOAD=true

export K3S_URL="https://$1:6443" 

cd ./shell
cp k3s /usr/local/bin
cp registries.yaml /etc/rancher/k3s/registries.yaml
sed -i "s/ip/$3/g" /etc/rancher/k3s/registries.yaml
sed -i "s/port/$4/g" /etc/rancher/k3s/registries.yaml
sed -i "s/userne/$5/g" /etc/rancher/k3s/registries.yaml
sed -i "s/passwd/$6/g" /etc/rancher/k3s/registries.yaml
chmod u+x /usr/local/bin/k3s

./install.sh